//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.ProviderException;
import java.security.Provider.Service;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Iterator;
import java.util.List;
import sun.security.jca.GetInstance;
import sun.security.jca.GetInstance.Instance;
import sun.security.util.Debug;

public class Mac implements Cloneable {
    private static final Debug debug = Debug.getInstance("jca", "Mac");
    private static final Debug pdebug = Debug.getInstance("provider", "Provider");
    private static final boolean skipDebug = Debug.isOn("engine=") && !Debug.isOn("mac");
    private Provider provider;
    private javax.vpncrypto.MacSpi spi;
    private final String algorithm;
    private boolean initialized = false;
    private Service firstService;
    private Iterator<Service> serviceIterator;
    private final Object lock;
    private static int warnCount = 10;

    protected Mac(javax.vpncrypto.MacSpi var1, Provider var2, String var3) {
        this.spi = var1;
        this.provider = var2;
        this.algorithm = var3;
        this.serviceIterator = null;
        this.lock = null;
    }

    private Mac(Service var1, Iterator<Service> var2, String var3) {
        this.firstService = var1;
        this.serviceIterator = var2;
        this.algorithm = var3;
        this.lock = new Object();
    }

    public final String getAlgorithm() {
        return this.algorithm;
    }

    public static final Mac getInstance(String var0) throws NoSuchAlgorithmException {
        List var1 = GetInstance.getServices("Mac", var0);
        Iterator var2 = var1.iterator();

        Service var3;
        do {
            if (!var2.hasNext()) {
                throw new NoSuchAlgorithmException("Algorithm " + var0 + " not available");
            }

            var3 = (Service)var2.next();
        } while(!javax.vpncrypto.JceSecurity.canUseProvider(var3.getProvider()));

        return new Mac(var3, var2, var0);
    }

    public static final Mac getInstance(String var0, String var1) throws NoSuchAlgorithmException, NoSuchProviderException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("Mac", javax.vpncrypto.MacSpi.class, var0, var1);
        return new Mac((javax.vpncrypto.MacSpi)var2.impl, var2.provider, var0);
    }

    public static final Mac getInstance(String var0, Provider var1) throws NoSuchAlgorithmException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("Mac", javax.vpncrypto.MacSpi.class, var0, var1);
        return new Mac((javax.vpncrypto.MacSpi)var2.impl, var2.provider, var0);
    }

    void chooseFirstProvider() {
        if (this.spi == null && this.serviceIterator != null) {
            synchronized(this.lock) {
                if (this.spi == null) {
                    if (debug != null) {
                        int var2 = --warnCount;
                        if (var2 >= 0) {
                            debug.println("Mac.init() not first method called, disabling delayed provider selection");
                            if (var2 == 0) {
                                debug.println("Further warnings of this type will be suppressed");
                            }

                            (new Exception("Call trace")).printStackTrace();
                        }
                    }

                    NoSuchAlgorithmException var8 = null;

                    while(true) {
                        Service var3;
                        do {
                            if (this.firstService == null && !this.serviceIterator.hasNext()) {
                                ProviderException var9 = new ProviderException("Could not construct MacSpi instance");
                                if (var8 != null) {
                                    var9.initCause(var8);
                                }

                                throw var9;
                            }

                            if (this.firstService != null) {
                                var3 = this.firstService;
                                this.firstService = null;
                            } else {
                                var3 = (Service)this.serviceIterator.next();
                            }
                        } while(!javax.vpncrypto.JceSecurity.canUseProvider(var3.getProvider()));

                        try {
                            Object var4 = var3.newInstance((Object)null);
                            if (var4 instanceof javax.vpncrypto.MacSpi) {
                                this.spi = (javax.vpncrypto.MacSpi)var4;
                                this.provider = var3.getProvider();
                                this.firstService = null;
                                this.serviceIterator = null;
                                return;
                            }
                        } catch (NoSuchAlgorithmException var6) {
                            var8 = var6;
                        }
                    }
                }
            }
        }
    }

    private void chooseProvider(Key var1, AlgorithmParameterSpec var2) throws InvalidKeyException, InvalidAlgorithmParameterException {
        synchronized(this.lock) {
            if (this.spi != null) {
                this.spi.engineInit(var1, var2);
            } else {
                Exception var4 = null;

                while(true) {
                    Service var5;
                    do {
                        do {
                            if (this.firstService == null && !this.serviceIterator.hasNext()) {
                                if (var4 instanceof InvalidKeyException) {
                                    throw (InvalidKeyException)var4;
                                }

                                if (var4 instanceof InvalidAlgorithmParameterException) {
                                    throw (InvalidAlgorithmParameterException)var4;
                                }

                                if (var4 instanceof RuntimeException) {
                                    throw (RuntimeException)var4;
                                }

                                String var10 = var1 != null ? var1.getClass().getName() : "(null)";
                                throw new InvalidKeyException("No installed provider supports this key: " + var10, var4);
                            }

                            if (this.firstService != null) {
                                var5 = this.firstService;
                                this.firstService = null;
                            } else {
                                var5 = (Service)this.serviceIterator.next();
                            }
                        } while(!var5.supportsParameter(var1));
                    } while(!javax.vpncrypto.JceSecurity.canUseProvider(var5.getProvider()));

                    try {
                        javax.vpncrypto.MacSpi var6 = (javax.vpncrypto.MacSpi)var5.newInstance((Object)null);
                        var6.engineInit(var1, var2);
                        this.provider = var5.getProvider();
                        this.spi = var6;
                        this.firstService = null;
                        this.serviceIterator = null;
                        return;
                    } catch (Exception var8) {
                        if (var4 == null) {
                            var4 = var8;
                        }
                    }
                }
            }
        }
    }

    public final Provider getProvider() {
        this.chooseFirstProvider();
        return this.provider;
    }

    public final int getMacLength() {
        this.chooseFirstProvider();
        return this.spi.engineGetMacLength();
    }

    public final void init(Key var1) throws InvalidKeyException {
        try {
            if (this.spi != null) {
                this.spi.engineInit(var1, (AlgorithmParameterSpec)null);
            } else {
                this.chooseProvider(var1, (AlgorithmParameterSpec)null);
            }
        } catch (InvalidAlgorithmParameterException var3) {
            throw new InvalidKeyException("init() failed", var3);
        }

        this.initialized = true;
        if (!skipDebug && pdebug != null) {
            pdebug.println("Mac." + this.algorithm + " algorithm from: " + this.provider.getName());
        }

    }

    public final void init(Key var1, AlgorithmParameterSpec var2) throws InvalidKeyException, InvalidAlgorithmParameterException {
        if (this.spi != null) {
            this.spi.engineInit(var1, var2);
        } else {
            this.chooseProvider(var1, var2);
        }

        this.initialized = true;
        if (!skipDebug && pdebug != null) {
            pdebug.println("Mac." + this.algorithm + " algorithm from: " + this.provider.getName());
        }

    }

    public final void update(byte var1) throws IllegalStateException {
        this.chooseFirstProvider();
        if (!this.initialized) {
            throw new IllegalStateException("MAC not initialized");
        } else {
            this.spi.engineUpdate(var1);
        }
    }

    public final void update(byte[] var1) throws IllegalStateException {
        this.chooseFirstProvider();
        if (!this.initialized) {
            throw new IllegalStateException("MAC not initialized");
        } else {
            if (var1 != null) {
                this.spi.engineUpdate(var1, 0, var1.length);
            }

        }
    }

    public final void update(byte[] var1, int var2, int var3) throws IllegalStateException {
        this.chooseFirstProvider();
        if (!this.initialized) {
            throw new IllegalStateException("MAC not initialized");
        } else {
            if (var1 != null) {
                if (var2 < 0 || var3 > var1.length - var2 || var3 < 0) {
                    throw new IllegalArgumentException("Bad arguments");
                }

                this.spi.engineUpdate(var1, var2, var3);
            }

        }
    }

    public final void update(ByteBuffer var1) {
        this.chooseFirstProvider();
        if (!this.initialized) {
            throw new IllegalStateException("MAC not initialized");
        } else if (var1 == null) {
            throw new IllegalArgumentException("Buffer must not be null");
        } else {
            this.spi.engineUpdate(var1);
        }
    }

    public final byte[] doFinal() throws IllegalStateException {
        this.chooseFirstProvider();
        if (!this.initialized) {
            throw new IllegalStateException("MAC not initialized");
        } else {
            byte[] var1 = this.spi.engineDoFinal();
            this.spi.engineReset();
            return var1;
        }
    }

    public final void doFinal(byte[] var1, int var2) throws javax.vpncrypto.ShortBufferException, IllegalStateException {
        this.chooseFirstProvider();
        if (!this.initialized) {
            throw new IllegalStateException("MAC not initialized");
        } else {
            int var3 = this.getMacLength();
            if (var1 != null && var1.length - var2 >= var3) {
                byte[] var4 = this.doFinal();
                System.arraycopy(var4, 0, var1, var2, var3);
            } else {
                throw new ShortBufferException("Cannot store MAC in output buffer");
            }
        }
    }

    public final byte[] doFinal(byte[] var1) throws IllegalStateException {
        this.chooseFirstProvider();
        if (!this.initialized) {
            throw new IllegalStateException("MAC not initialized");
        } else {
            this.update(var1);
            return this.doFinal();
        }
    }

    public final void reset() {
        this.chooseFirstProvider();
        this.spi.engineReset();
    }

    public final Object clone() throws CloneNotSupportedException {
        this.chooseFirstProvider();
        Mac var1 = (Mac)super.clone();
        var1.spi = (MacSpi)this.spi.clone();
        return var1;
    }
}
